
// This function generates and holds the list of contacts.
// It can be used to search up a nickname we have 'cached',
// or to see if someone is online.

var ChatboxContacts = new Class({
		
	// Specify the contacts list
	initialize: function()
	{
		this.contacts = new Array();
		
	}, // end function initialize
	
	// Reset the contacts list on logout.
	reset: function()
	{
		this.contacts = new Array();
		
	},
	
	// This function checks whether we have cached a contact already!
	exists: function(contact)
	{
		if (this.contacts[contact] == null)
			return true;
		else
			return false;
			
	},
	
	// Change a field of a contact.
	setChanges: function(contact, field, value)
	{
		// Does this contact exist?
		if (this.exists(contact))
			return;
		
		// Update the field.
		this.contacts[contact][field] = value;
		
	}, // end function setChanges
	
	// This function saves the contact list upon login.
	saveList: function(d)
	{
		// The syntax we get per line is like so:
		// contact,status,detailed_status,nickname
		var data = d.split("\n");
		var contact;
		
		var contactID;
		var status;
		var nickname;
		
		// Go through everything
		for (var i = 0; i < data.length; i++)
		{
			// Split the line on our seperator.
			contact = data[i].split(",");
			
			// Retrieve some data.
			contactID = contact[0];
			status = contact[1];
			de_status = contact[2];
			nickname = "";
			
			// Now we need the nickname. Ofcourse, it should have abilities to have a , in it.. so check if it has.
			// It has a , in it when we have more than 4 elements in our contact array
			if (contact.length > 4)
			{
				// It is. So, we can list all the elemnts from element 3 (the start) till the end.
				for (var j = 3; j < contact.length; j++)
					// Don't add a , when it's the first element
					if (nickname == "")
						nickname += contact[j];
					else
						nickname += "," + contact[j];
			} // end else
			else
				// Just get the nickname out.
				nickname = contact[3];
			
			// Save the contact.
			this.contacts[contactID] = {id: contactID, nickname: nickname, detailed_status: de_status, status: status};
			
		} // end for
		
	}, // end function setAllChanges
	
	// Retrieve the contacts list.
	update: function()
	{
		// TODO: Make great contacts update support
		var k = null;
		var v = null;
		
		// No contacts? Then we need to do a full update.
		if (this.contacts.length == 0)
		{
			k = ['i'];
			v = [1];
			
		} // end if
		
		// Request the contacts
		chat.getClsAjaxReq().request('contacts', k, v, function(t)
		{
			// Save the list and render the contacts list.
			chat.getChat().getClsContacts().saveList(t);
			chat.getChat().getClsContacts().render();
			
		}, null, 2);
		
		
	}, // end function update
	
	// This function renders the contact list.
	// This is only triggered upon changes.
	render: function()
	{
		var tmp = "";
		var contact;
		
		// Go through all contacts.
		this.contacts.forEach(function(contact)
		{
			// When the contact is invalid, skip.
			if (contact == null)
				return;
			
			// Retrieve the icon, and specify the detailed status when the user is online.
			var extra = "";
			var icon = chat.getChat().getClsContacts().getStatusIcon(contact['status'].toInt());
			
			if (icon != 'offline')
				extra = ' (' + contact['detailed_status'] + ')';
			
			// Add the contact to the temporary variable
			tmp += '<img src="ui/icons/status_' + icon + '.gif"> <a href="#" onClick="chat_start(\'' + contact['id'] + '\', \'s\');">' + contact['nickname'] + '</a>' + extra + '<br />';
			
		}); // end foreach
		
		// Set the contact list to the div.
		$('contactsList').innerHTML = tmp;
		
	}, // end function render
	
	// This function gets the status icon (image) from state number
	getStatusIcon: function(s)
	{
		s = parseInt(s);
		// Standard icon is offline when nothing else is specified.
		var icon = "offline";
		
		switch (s)
		{
			case 1: icon = 'online'; break;
			case 2: icon = 'away'; break;
			case 3: icon = 'busy'; break;
			
		} // end switch
		
		return icon;
		
	}, // end function
		
	// Get the state number from the icon
	getStatusNo: function(s)
	{
		var no = 0;
		
		switch (s)
		{
			case 'online': no = 1; break;
			case 'away': no = 2; break;
			case 'busy': no = 3; break;
			case 'offline': no = 4; break; // Actually.. offline is s<=0 AND s=4
			
		} // end switch
		
		return no;
		
	}, // end function
	
	// Check whether the user is online
	isOnline: function(c)
	{
		// Does it exist?
		if (this.exists(c))
		{
			// Offline is s<=0 OR s=4
			if (this.contacts[c]['status'] <= 0 || this.contacts[c]['status'] == 4)
				return false;
			else
				return true;
				
		} // end if
	
		return false;
		
	} // end function isOnline
	
});

// Yay, we 've loaded the contacts library
cimport_loaded('chatbox.Contacts');
